/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.impl;

import edu.upf.grupE.Controller.VideoController;
import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Entities.Video;
import edu.upf.grupE.Listener;
import java.util.Vector;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 *
 * @author xday666
 */
public class VideoListListener implements Listener {
    /*this class implements the listener which will be prepared to understand the searchProfile
    Packets.*/

    private IM im;

    /**
     *
     * @param instancem
     */
    public VideoListListener(final IM instancem) {
        im = instancem;
    }

    /**
     * we only work with the packets that are Messages. then we create a new Message
     * that will be the response to the search profile Message.
     * To be sure that the information that arrives to this listener is correct, we
     * print it into the Text Box.
     * Later we send back to the origin a Message containing the same object received.
     * @param packet
     */
    public final void processPacket(final Packet packet) {
        if (packet instanceof Message) {



            Message msg = (Message) packet;



            System.out.println(msg.getFrom() + " quiere mi lista de videos.");


            Vector<Video> videos = VideoController.getAll();
            System.out.println("Recibida peticion de: " + msg.getFrom() + " enviando " + videos.size() +" resultados.");

            for (Video tmp : videos) {
                Message searchResult = new Message();
                searchResult.setTo(msg.getFrom());

                Video temp = tmp;


                searchResult.setProperty("searchResultVideo", temp);
                searchResult.setPacketID(Config.PACKETLISTVIDEOSRESULTID);
                im.send(searchResult);
            }


        }
    }
}
